package cn.com.thinvent.dispatch.web.controller;

import cn.com.thinvent.dispatch.entity.MenuManagement;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.entity.response.ReturnModel;
import cn.com.thinvent.dispatch.service.biz.MenuManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuManagementController {

    @Autowired
    private MenuManagementService menuManagementService;

    @GetMapping("/page")
    public ReturnModel selectPage(
            @RequestParam int page,
            @RequestParam int limit,
            @RequestParam(required = false) String cuisineType) {
        int offset = (page - 1) * limit;
        List<MenuManagement> results = menuManagementService.selectPage(offset, limit, cuisineType);
        int totalRecords = menuManagementService.count(cuisineType);

        PagedGridResult<MenuManagement> pagedGridResult = new PagedGridResult<>();
        pagedGridResult.setPage(page);
        pagedGridResult.setRows(results);
        pagedGridResult.setRecords(totalRecords);
        pagedGridResult.setTotal((int) Math.ceil((double) totalRecords / limit));

        ReturnModel returnModel = new ReturnModel(200, pagedGridResult, "Success");
        return returnModel;
    }

    @GetMapping("/batchGet")
    public List<MenuManagement> getMenuManagementListByIds(@RequestParam String ids) {
        List<String> strings = Arrays.asList(ids.split(","));
        return menuManagementService.findMenuManagementsByIds(strings);
    }
}

